package com.lsw.util;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.widget.Button;


import com.lsw.dialog.DateTimePickerDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 */
public class TimeUtil {
    private Context context;

    public void TimeUtil(Context context) {
        this.context = context;
    }

    /**
     * 显示时间选择器
     *
     * @param button
     */
    public void changeDateFormat(final Button button) {
        DateTimePickerDialog timePickerDialog = new DateTimePickerDialog(
                context, System.currentTimeMillis());
        timePickerDialog.setOnDateTimeSetListener(new DateTimePickerDialog.OnDateTimeSetListener() {
            public void OnDateTimeSet(AlertDialog dialog, long date) {
                button.setText(getStringDate(date));
            }
        });
        timePickerDialog.show();
    }

    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm
     */
    @SuppressLint("SimpleDateFormat")
    private String getStringDate(Long date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 默认返回当前时间
     *
     * @param phpTimeStamp php时间戳
     * @param timeFormat   返回时间格式 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getSimpleDate(String phpTimeStamp, String timeFormat) {
        Date date = null;
        try {
            if (phpTimeStamp != null && phpTimeStamp.length() == 10) {
                date = new Date(Long.parseLong(phpTimeStamp + "000"));
            } else {
                date = new Date(Long.parseLong(phpTimeStamp));
            }
        } catch (Exception e) {
            e.printStackTrace();
            date = new Date();
        }
        SimpleDateFormat format = new SimpleDateFormat(
                TextUtils.isEmpty(timeFormat) ? "yyyy-MM-dd" : timeFormat);
        String format2 = format.format(date);
        return format2;
    }

    /**
     * 根据秒数获取剩余时间 x天x小时x分
     *
     * @param total_seconds
     * @return
     */
    public static String timePhpTime(long total_seconds) {
        if (total_seconds <= 0) {
            return "0天";
        }
        long days = Math.abs(total_seconds / (24 * 60 * 60));
        long hours = Math.abs((total_seconds % (24 * 60 * 60)) / (60 * 60));
        long minutes = Math.abs((total_seconds % (24 * 60 * 60)) % (60 * 60) / 60);
        String leftTime = "";
        if (days > 0) {
            leftTime += days + "天";
        }
        if (hours > 0) {
            leftTime += hours + "小时";
        }
        if (minutes > 0) {
            leftTime += minutes + "分钟";
        }
        if (leftTime.equals("")) {
            return "0";
        } else
            return leftTime;
    }

    /**
     * 返回格式 00:00:00
     *
     * @param time
     * @return
     */
    public static String toHHMMSS(int time) {
        String times = "";
        int h, m, s;
        time = time / 1000;
        h = time / 3600;
        m = time % 3600 / 60;
        s = time % 3600 % 60;
        if (h < 10) {
            times = "0" + h + ":";
        } else {
            times = h + ":";
        }
        if (m < 10) {
            times += "0" + m + ":";
        } else {
            times += m + ":";
        }
        if (s < 10) {
            times += "0" + s;
        } else {
            times += s + "";
        }
        return times;
    }

    /**
     * 返回格式 0:00
     *
     * @param time
     * @return
     */
    public static String toMSS(int time) {
        if (time >= 600)
            return "0:00";
        String times = "";
        int m, s;
        m = time / 60;
        s = time % 60;
        times += m + ":";
        if (s < 10) {
            times += "0" + s;
        } else {
            times += s + "";
        }
        return times;
    }

    /**
     * 根据月份获取天数
     *
     * @param mouth  指定起始月份
     * @param number 月数
     * @return 返回天数
     */
    public static int GetDays(int yeas, int mouth, int number) {
        LogUtils.i("111111yeas=", yeas + "mouth=" + mouth + "number=" + number);
        int allday = 0;
        for (int i = 0; i < number; i++) {
            allday += getDay(yeas + (mouth + i) / 12, (mouth + i) % 12);
        }
        LogUtils.i("111111allday=", allday + "");
        return allday;
    }

    /**
     * 根据月份获取天数
     *
     * @param mouth
     * @return
     */
    public static int getDay(int yeas, int mouth) {
        int day = 0;
        switch (mouth) {
            case 1:
                day = 31;
                break;
            case 2:
                if (yeas % 4 == 0) {
                    day = 29;
                } else {
                    day = 28;
                }
                break;
            case 3:
                day = 31;
                break;
            case 4:
                day = 30;
                break;
            case 5:
                day = 31;
                break;
            case 6:
                day = 30;
                break;
            case 7:
                day = 31;
                break;
            case 8:
                day = 31;
                break;
            case 9:
                day = 30;
                break;
            case 10:
                day = 31;
                break;
            case 11:
                day = 30;
                break;
            case 12:
                day = 31;
                break;
        }
        return day;
    }

    /**
     * 获取当前号数
     *
     * @param yeas
     * @param mouth
     * @param day
     * @return
     */
    public static int day(int yeas, int mouth, int day) {
        int days;
        if (day <= getDay(yeas, mouth)) {
            days = day;
        } else {
            days = getDay(yeas, mouth);
        }
        return days;
    }

    /**
     * 获取当月天数
     *
     * @return
     */
    public static int mouthday() {
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        return getDay(year, month);
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static long getTime() {
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳
        return time;
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static long getTimess() {
        long time = System.currentTimeMillis();//获取系统时间的10位的时间戳
        return time;
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime() / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getYearByTimeStamp(long timeStamp) {
        String date = timeStampToDate(timeStamp);
        String year = date.substring(0, 4);
        return Integer.parseInt(year);
    }

    public static int getMonthByTimeStamp(long timeStamp) {
        String date = timeStampToDate(timeStamp);
        String month = date.substring(5, 7);
        return Integer.parseInt(month);
    }

    public static int getDayByTimeStamp(long timeStamp) {
        String date = timeStampToDate(timeStamp);
        String day = date.substring(8, 10);
        return Integer.parseInt(day);
    }

    public static String timeStampToDate(long timeStamp) {
        Date date = new Date(timeStamp * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }

    //判断两个时间戳是否为同一天
    public static boolean isTwoTimeStampDayEqual(long firstTimeStamp, long secondTimeStamp) {
        if (getYearByTimeStamp(firstTimeStamp) == getYearByTimeStamp(secondTimeStamp) &&
                getMonthByTimeStamp(firstTimeStamp) == getMonthByTimeStamp(secondTimeStamp)
                && getDayByTimeStamp(firstTimeStamp) == getDayByTimeStamp(secondTimeStamp)) {
            return true;
        }
        return false;
    }
}
